﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Threading.Tasks;
using DailyDiet.Models;
using DailyDiet.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Xamarin.Forms;

namespace DailyDiet.ViewModels
{
/*菜品推荐反馈页面
 *
 * 推荐算法个性推荐的
 */
    public class DietFeedbackViewModel : ViewModelBase {

        private IPreferenceStorage _preference;

        private ObservableCollection<FeedbackObject> _referenceCollection;

        public ObservableCollection<FeedbackObject> ReferenceCollection {
            get => _referenceCollection;
            set => Set(nameof(ReferenceCollection), ref _referenceCollection, value);
        }

        private ObservableCollection<FeedbackObject> _feedbacks;

        private string _feedbackText;

        public string FeedbackText {
            get => _feedbackText;
            set => Set(nameof(FeedbackText), ref _feedbackText, value);
        }

        public ObservableCollection<FeedbackObject> Feedbacks {
            get => _feedbacks;
            set => Set(nameof(Feedbacks), ref _feedbacks, value);
        }


        private RelayCommand<FeedbackObject> _addFeedbackCommand;

        public RelayCommand<FeedbackObject> AddFeedbackCommand =>
            _addFeedbackCommand ?? (_addFeedbackCommand =
                new RelayCommand<FeedbackObject>((feedback) => AddFeedbackCommandFunc(feedback)));

        public void AddFeedbackCommandFunc(FeedbackObject feedback) {
            Feedbacks.Add(feedback);
            ReferenceCollection.Remove(feedback);
        }

        private RelayCommand<FeedbackObject> _subFeedbackCommand;

        public RelayCommand<FeedbackObject> SubFeedbackCommand =>
            _subFeedbackCommand ?? (_subFeedbackCommand =
                new RelayCommand<FeedbackObject>((feedback) => SubFeedbackCommandFunc(feedback)));

        public void SubFeedbackCommandFunc(FeedbackObject feedback)
        {
            Feedbacks.Remove(feedback);
            ReferenceCollection.Add(feedback);
        }
        private RelayCommand _confirmCommand;

        public RelayCommand ConfirmCommand =>
            _confirmCommand ?? (_confirmCommand =
                new RelayCommand(async () => await ConfirmCommandFunc()));

        public async Task ConfirmCommandFunc() {
            ModifyIndex();

            await AppShell.Current.DisplayAlert(
                "Alert",
                "感谢您的反馈，我们将根据您的反馈做出调整",
                "OK");
            await Shell.Current.CurrentPage.Navigation.PopAsync();
        }

        public void ModifyIndex() {
            foreach (var feedbackObject in Feedbacks)
            {
                _preference.Set(feedbackObject.IndexKey,
                    _preference.Get(feedbackObject.IndexKey, 0) +
                    feedbackObject.Index);
            }
        }

        private RelayCommand _appearingCommand;

        public RelayCommand AppearingCommand =>
            _appearingCommand ?? (_appearingCommand =
                new RelayCommand(() => AppearingCommandFunc()));

        public void AppearingCommandFunc() {
            //初始化两个集合
            Feedbacks = new ObservableCollection<FeedbackObject>();
            var referenceCollection =
                new ObservableCollection<FeedbackObject>() {
                    FeedBackObjectConstant.NotFavoriteFeedback,
                    FeedBackObjectConstant.SoFatFeedback,
                    FeedBackObjectConstant.SoSpicyFeedback,
                    FeedBackObjectConstant.SoSweetFeedback
                };
            ReferenceCollection = referenceCollection;

        }

        public DietFeedbackViewModel(
            IPreferenceStorage preference) {
            _preference = preference;
        }
    }
}
